<?php

/*
    Open Designs Content Management System (odCMS)
    Copyright (C) 2010  Thomas Wong - tom@ztml.org

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
*/

if($_GET['type'] == "info")
{
        phpinfo();
        exit;
}

if( !file_exists("./_main/index.php") )
{
	echo "
It appears that <b>odCMS</b> has not been installed.<br /><br />
Please run <a href='install.php'>install.php</a> to install it.<br /><br />
Contact <a href='http://odcms.net/?usite=_wdn&Page=Contact Us'>odCMS Administrator</a> if you have any questions or problems.";
	exit;
}

session_start();

$_SESSION['usite'] = $usite = userSite("_main"); // _main is the default name for this site

// if odcms is not installed in the root directory
if($_SERVER["DOCUMENT_ROOT"]!=getcwd())
{
	preg_match("/.*([\\\|\/])([a-zA-Z0-9_]+)$/", getcwd(), $dirs);
	if($dirs[2] != "public_html") // update this if the root directory has a different name
	{
		$_SESSION['usite'] = "";
		header("location: /$dirs[2]/$usite/?index.php");
		echo "<h3>Error encountered while redirecting...</h3><a href='/'>Click here to continue...</a>";
		exit;
	}
}
// 
$DIR = "./codes/";

if(file_exists("./$usite/cc_functions.php") )
{
	include_once ("./$usite/cc_functions.php");
}
else
{
	include_once ($DIR.'cc_functions.php');
}
include_once ($DIR.'cc_init.php');
include_once ($DIR.'cc_io.php');
include_once ($DIR.'cc_class.php');
include_once ($DIR.'cc_update.php');

if($_REQUEST['Update']) new contentUpdate();
else new contentControl();

exit;

/*--------------------------\
| get/set/update user site  |
\--------------------------*/
function userSite($default)
{
	$sname = preg_replace("/www\./","",$_SERVER['SERVER_NAME']);
	
	$thisSITES = new SQLiteDatabase("./sites.dbf") or die("could not open SQLiteDatabase"); // members database
	
	$q = @$thisSITES->query("SELECT Site FROM sites WHERE Name='$sname'");
	if($q)
	{
		list($usite) = $q->fetch();
	}

	if(!$_REQUEST['_update_'] || $_SESSION['UserName']!="admin")
	{
		if(!$usite && $_REQUEST['usite']) $usite = $_REQUEST['usite'];
		if(!$usite)
		{
			$usite = $_SESSION['usite'];
			if(!$usite) $usite = $default;
		}
		if(! is_dir($usite))
		{
			$usite = $default;
		}
		$_SESSION['usite'] = $usite;
		
		return $usite;
	}
	
	if($_REQUEST['_update_'] == "add")
	{
		$site = $_REQUEST['site'];
		$q = $thisSITES->query("SELECT Name FROM sites WHERE Site='$site'");
		list($name) = $q->fetch();
		if($name)
		{
			echo "$site with domain named $name is already in the database!";
			exit;
		}
		$name = preg_replace("/www\./","",$_REQUEST['name']);
		$desc = $_REQUEST['desc'];
		if(!$name || !$site)
		{
			echo "Required info is missing!<br />($name) ($site) ($desc)";
			exit;
		}
		$thisSITES->query("INSERT INTO sites VALUES('$name','$site','$desc')");
		echo "$name for $site is added into the database";
		exit;
	}
	if($_REQUEST['_update_'] == "dlt")
	{
		$site = $_REQUEST['site'];
		$q = $thisSITES->query("SELECT Name FROM sites WHERE Site='$site'");
		list($name) = $q->fetch();
		if(!$name)
		{
			echo "$site is not in the database!";
			exit;
		}
		$q = $thisSITES->query("DELETE FROM sites WHERE Site='$site'");
		echo "$name is deleted from the database for $site!";
		exit;
	}
	if($_REQUEST['_update_'] == "chg")
	{
		$site = $_REQUEST['site'];
		$q = $thisSITES->query("SELECT Name FROM sites WHERE Site='$site'");
		list($name) = $q->fetch();
		if(!$name)
		{
			echo "$site is not in the database!";
			exit;
		}
		$newname = $_REQUEST['name'];
		$q = $thisSITES->query("UPDATE sites SET Name='$newname' WHERE Site='$site'");
		echo "Domain name for $site is renamed from $name to $newname in the database!";
		exit;
	}
	echo "<table><tr><th>Domain</th><th>Site</th><th>Description</th></tr>";
	$q = $thisSITES->query("SELECT * FROM sites ORDER BY Site");
	while( $q->valid() )
	{
		list($name,$site,$desc) = $q->current();

		echo "<tr><th>$name</th><th>$site</th><td>$desc</td></tr>\n";
			
		$q->next();
		}
	echo "</table>";
	exit;
}
 
?>

